package control.control.authmanage;


import base.bean.StandardResponse;
import base.standard.constant.message.MessageCode;
import base.util.BeanUtil;
import base.util.ResponseUtil;
import base.validation.groups.AuthSave;
import base.validation.groups.AuthUpdate;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import control.module.bo.authmanage.AuthBo;
import control.module.po.authmanage.Auth;
import control.module.vo.authmanage.AuthVO;
import control.service.IAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author cyj
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/auth")
@ResponseStatus(code = HttpStatus.OK)
@Slf4j
public class AuthController {

    @Autowired
    private IAuthService authService;

    @GetMapping("/all")
    public StandardResponse getAuths(Page page) {
        IPage<Auth> list = authService.page(page);
        return ResponseUtil.success(BeanUtil.newInstanceAndCopyBatch(list, AuthVO.class));
    }

    @PostMapping
    public StandardResponse saveRole(@RequestBody @Validated(AuthSave.class) AuthBo authBo) {
        Auth auth = BeanUtil.newInstanceAndCopy(authBo, Auth.class);
        boolean result = authService.save(auth);
        log.info("save auth result:[{}]", result);
        return ResponseUtil.success(MessageCode.AUTH_SAVE, result);
    }

    @PutMapping
    public StandardResponse updateRole(@RequestBody @Validated(AuthUpdate.class) AuthBo authBo) {
        Auth auth = BeanUtil.newInstanceAndCopy(authBo, Auth.class);
        boolean result = authService.updateById(auth);
        log.info("update auth id:[{}] result:[{}]", auth.getId(), result);
        return ResponseUtil.success(MessageCode.AUTH_UPDATE, result);
    }

    @DeleteMapping
    public StandardResponse deleteRole(@NotNull Integer id) {
        boolean result = authService.removeById(id);
        log.info("delete auth id:[{}] result:[{}]", id, result);
        return ResponseUtil.success(MessageCode.AUTH_DELETE, result);
    }

    @GetMapping
    public StandardResponse selectRole(@NotNull Integer id) {
        Auth auth = authService.getById(id);
        AuthVO authVO = BeanUtil.newInstanceAndCopy(auth, AuthVO.class);
        return ResponseUtil.success(authVO);
    }

}

